// pages/familyFeast/orderDetail.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "家宴", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl:'/pages/familyFeast/orderList'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,

    openid:'',
    id:0,
    shareTitle:'',
    shareImg:'',
    orderInfo:[],
    foodList:[],
    lxTel:'',//联系电话
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  tel(e){
    console.log(e)
    let phone = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  //用户取消订单
  cancelJyOrder(){
    var that=this;
    wx.showModal({  
      title: '确认',  
      content: '确定要取消此订单吗？',  
      success (res) {  
        if (res.confirm) {  
           wx.showLoading({
          title: '订单取消中...'
        });

        wx.request({
          url: that.data.baseUrl + 'cancelJyOrder.php',
          method: 'POST',
          data: {
            openid: that.data.openid,
            id:that.data.id 
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            wx.hideLoading();
             app.showTip(res.data.msg);
            if (res.data.status == 1) {
              //后台请求成功
              var orderInfo = that.data.orderInfo
              orderInfo.sh_status=3;
              that.setData({
                orderInfo:orderInfo
              });
            }
        },
        fail: function (res) {
          wx.hideLoading();
          app.showTip('服务器繁忙,请稍候重试');
        }
      });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    //获取页面参数
    var id = options.id;
    //id =2;
    if(id==''||id==undefined){
      app.showTip('抱歉,未获取到订单id哦');
      setTimeout(function () {
        wx.redirectTo({
          url: '/pages/familyFeast/orderList'
        });
      }, 1400);
      return;
    }else{
      //保存订单id
      that.setData({
        id:id
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');
    
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getFamilyFeastOrderDetailPageInfo(res.data.openid, id);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getFamilyFeastOrderDetailPageInfo(openid, id);
    }

    //调用分享
    that.getShareInfo();
  },

    //获取本页数据
  getFamilyFeastOrderDetailPageInfo(openid, id) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getFamilyFeastOrderDetailPageInfo.php',
      method: 'POST',
      data: {
        openid: openid,
        id:id 
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          that.setData({
            orderInfo:res.data.orderInfo,
            foodList:res.data.foodList,
            lxTel:res.data.lxTel
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/familyFeast/orderList'
            });
          }, 2000);
          return;
        }
      },
      fail: function (res) {
        wx.hideLoading();
        app.showTip('服务器繁忙,请稍候重试');
        setTimeout(function () {
          wx.redirectTo({
              url: '/pages/familyFeast/orderList'
          });
        }, 2000);
        return;
      }
    });
  },


    //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo_order.php',
      method: 'POST',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/familyFeast/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})